<template>
  <div class='page-content'
       :style="bgStyle">

    <!-- 工具栏区域 -->
    <table-bar :showTop="false"
               :columns="columns"
               @changeColumn="changeColumn">
      <!-- 条件查询区域 -->
      <div slot="top">

      </div>
      <!-- 左侧按钮 -->
      <div slot="bottom">
        <el-button type="primary"
                   icon="el-icon-upload"
                   plain
                   @click="showBlockUpDialog">数据上链</el-button>
      </div>

    </table-bar>

    <!-- 表格区域 -->
    <the-table :data="blockChainList"
               :showPage="false"
               @getDataBySelectBox="handleSelectionChange"
               ref="table">
      <el-table-column label="ID"
                       prop="id"
                       v-if="columns[0].show" />
      <el-table-column label="数据类型"
                       prop="dataType"
                       v-if="columns[1].show" />
      <el-table-column label="数据"
                       v-if="columns[2].show">
        <template slot-scope="{row}">
          <span class="text_truncation">{{row.data}}</span>
          <i class="el-icon-caret-right"
             @click="showDetailsDialog(row.data)"></i>
        </template>
      </el-table-column>
      <el-table-column label="当前节点Hash"
                       min-width="100px"
                       prop="currentblockinfo"
                       v-if="columns[3].show" />
      <el-table-column label="前节点Hash"
                       min-width="100px"
                       prop="previousblockinfo"
                       v-if="columns[4].show" />
      <el-table-column label="链高度"
                       sortable
                       prop="blockheight"
                       v-if="columns[5].show" />
      <el-table-column label="溯源码网址"
                       prop="traceabilityAddress"
                       v-if="columns[6].show" />
      <el-table-column label="Hash寻址码"
                       prop="hashAddress"
                       v-if="columns[7].show" />
      <el-table-column label="溯源二维码"
                       v-if="columns[8].show">
        <template slot-scope="{row}">
          <vue-qr :text="row.traceabilityAddress"
                  width="100%" />
        </template>
      </el-table-column>

      <el-table-column label="操作"
                       fixed="right"
                       width="200px"
                       v-if="columns[9].show">
        <template slot-scope="{row}">
          <el-button type="text"
                     icon="el-icon-set-up"
                     @click="compare(row)">对比
          </el-button>
          <el-button type="text"
                     style="color: #FA6962"
                     icon="el-icon-delete"
                     @click="delById(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </the-table>

    <!-- 分页器区域 -->
    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :page-sizes="[5, 8, 15,20]"
                   :page-size="pageSize"
                   :current-page="pageNum"
                   :total="total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   class="page">
    </el-pagination>
    <!-- 数据上链 -->
    <el-dialog title="数据上链"
               :visible.sync="blockUpDialog"
               width="40%">
      <div>
        <el-form :model="addForm"
                 :rules="addFormRules"
                 ref="addFormRef"
                 label-width="60px">
          <el-form-item label="名称"
                        prop="name">
            <el-input v-model="addForm.name"
                      clearable></el-input>
          </el-form-item>
          <el-form-item label="数据"
                        prop="name">
            <el-input type="textarea"
                      clearable
                      v-model="addForm.name"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer">
        <el-button @click="blockUpDialog = false">取 消</el-button>
        <el-button type="primary"
                   @click="blockUpDialog = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 对比结果 -->
    <el-dialog title="比对结果"
               :visible.sync="resultDialog"
               width="40%">
      <div class="dialog_result">
        <span :class="hashNotChange?'dialog_result_success':'dialog_result_warning'">
          {{hashNotChange?'hash值一致，未被篡改！':'hash值不一致，已被篡改！'}}
        </span>
        <img :src="resultImg"
             width="500px">
      </div>
      <span slot="footer">
        <el-button @click="resultDialog = false">取 消</el-button>
        <el-button type="primary"
                   @click="resultDialog = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 数据详情 -->
    <el-dialog title="数据详情"
               :visible.sync="detailsDialog"
               width="40%">
      <div>
        <el-descriptions title="加工"
                         :column="3"
                         border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-postcard"></i>
              流程名
            </template>
            {{details.processInfo.processName}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              负责人
            </template>
            {{details.processInfo.processPrincipal}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-location-outline"></i>
              加工时间
            </template>
            {{details.processInfo.processTime}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-tickets"></i>
              产品名
            </template>
            <el-tag size="small"
                    effect="plain"> {{details.processInfo.processingType}}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-shopping-cart-2"></i>
              数量
            </template>
            <el-tag size="small"
                    effect="plain"> {{details.processInfo.amount}}
            </el-tag>
          </el-descriptions-item>

        </el-descriptions>
        <br>
        <el-descriptions title="屠宰"
                         :column="2"
                         border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-postcard"></i>
              流程名
            </template>
            {{details.SlaughterInfo.processName}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              经手人
            </template>
            {{details.SlaughterInfo.handler}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-location-outline"></i>
              描述
            </template>
            <span v-for="(item,idx) in details.SlaughterInfo.processDescribe"
                  :key="item">
              <i class="el-icon-caret-right"
                 v-if="idx!==0"></i>
              <el-tag>
                {{item}}
              </el-tag>

              <!-- {{details.SlaughterInfo.processDescribe}} -->

            </span>
          </el-descriptions-item>
        </el-descriptions>

      </div>
      <span slot="footer">
        <el-button type="primary"
                   @click="detailsDialog = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { changeColumn, pagination, dialogClose, multipleSelection, queryForm } from '@/api/mixin/'
import VueQr from 'vue-qr'
import hashNotChange from '@image/hashNotChange.png'
import hashChange from '@image/hashChange.png'
// import bgTable from '@image/bg_table.png'
export default {
  name: "BlockChainMamage",
  meta: {
    title: "区块链管理",
  },
  mixins: [changeColumn, pagination, dialogClose, multipleSelection, queryForm],
  components: {
    VueQr
  },
  data () {
    return {
      columns: [
        { name: "ID", show: true },
        { name: "数据类型", show: true },
        { name: "数据", show: true },
        { name: "当前节点Hash", show: true },
        { name: "前节点Hash", show: true },
        { name: "链高度", show: true },
        { name: "溯源码网址", show: true },
        { name: "Hash寻址码", show: true },
        { name: "溯源二维码", show: true },
        { name: "操作", show: true },
      ],
      blockChainList: [],
      bgStyle: {
        // backgroundImage: `url(${bgTable})`
      },
      detailsDialog: false,
      resultDialog: false,
      blockUpDialog: false,
      addForm: { name: '' },
      addFormRules: {},
      hashNotChange: true,
      details: { processInfo: {}, SlaughterInfo: {} }
    }
  },
  computed: {
    select_processingId () {
      return this.$store.state.Usersetting.ProcessInfo.processingId || '1'
    },
    resultImg () {
      return hashNotChange ? hashNotChange : hashChange
    }
  },
  methods: {
    getList () {
      this.getBlockChainList()
    },
    async getBlockChainList () {
      const { data: res } = await this.$http.get(`/processing/blockChain/pageInfo/${this.pageNum}/${this.pageSize}`)
      if (res.statusCode !== 20000) return this.$message.error('获取区块链列表失败')

      this.blockChainList = res.data.records
      this.total = res.data.total
    },
    showBlockUpDialog () {
      this.blockUpDialog = true
    },
    showDetailsDialog (data) {
      const { data: info } = JSON.parse(data)
      // JSON数据解析
      console.log(info, 'info');
      const [J, T] = info || []
      console.log(J[0], 'j');
      console.log(T, 't');
      // 处理屠宰流程描述
      T.processDescribe = T.processDescribe.split('->')
      this.details.processInfo = J[0]
      this.details.SlaughterInfo = T


      this.detailsDialog = true
    },
    /* 删除 */
    delById ({ id }) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`/processing/blockChain/delete/${id}`)

        if (res.statusCode !== 20000) return this.$message.error('删除失败')

        this.$message.success('删除成功')
        this.getList()

      }).catch(() => {
        this.$message('已取消删除!')
      })
    },
    async compare ({ id, hashAddress }) {
      const cropid = `t_processing_blockchain-${id}`
      const { data: res } = await this.$http.get(`/processing/blockchain/hashaddress`, { params: { cropid, hashaddress: hashAddress } })

      if (res.statusCode !== 20000) return this.$message.error('查询失败')

      this.hashNotChange = res.flag
      this.resultDialog = true
    }
  },
  created () {
    this.getBlockChainList()
  }
}
</script>

<style lang="scss" scoped>
@import url('//unpkg.com/element-ui@2.15.7/lib/theme-chalk/index.css');
.page {
  margin-top: 6px;
  text-align: center;
}
.dialog_result {
  text-align: center;
  font-size: 30px;
  img {
    margin: -50px 0;
    z-index: -1;
  }
  &_success {
    color: green;
  }
  &_warning {
    color: red;
  }
}
.text_truncation {
  width: 200px;
  // height: 100px;
  // 宽度需要自己调整
  overflow: hidden;
  text-overflow: ellipsis;
  /* 弹性伸缩盒子模型显示 */
  display: -webkit-box;
  /* 限制在一个块元素显示的文本的行数 */
  -webkit-line-clamp: 4;
  /* 设置或检索伸缩盒对象的子元素的排列方式 */
  -webkit-box-orient: vertical;
}
</style>